package ird.demo.controller.transaction;

import ird.demo.common.DemoConstant;
import ird.demo.common.HotkeyUtil;
import ird.demo.domain.Menu;
import ird.demo.service.CommonService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller
@RequestMapping("/transaction")
public abstract class TranController {
	
	@Autowired
	private CommonService service;
	
	@Autowired
    protected MessageSource messageSource;
	
	protected Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
	
	protected abstract String getTc();
	
	protected abstract void preInput(ModelMap model);
	
	protected abstract void preDetail(HttpServletRequest request, HttpSession session, ModelMap model);
	
	public void clearSession(HttpSession session){
		session.removeAttribute("tranId");
		session.removeAttribute("tranDesc");
		session.removeAttribute("menuId");
		session.removeAttribute("menuDesc");
	}
	
	@RequestMapping("/input")
	protected String input(@RequestParam String tranDesc,HttpServletRequest request,HttpSession session,ModelMap model) {
		
		System.out.println("=================input");
		
		String keepKeyParamInd = request.getParameter(DemoConstant.KEEP_KEY_PARAM_IND);
		if("Y".equals(keepKeyParamInd) == false){
			session.removeAttribute(DemoConstant.KEY_PARAM);
		}
		else{
			model.addAttribute(DemoConstant.KEY_PARAM, session.getAttribute(DemoConstant.KEY_PARAM));
		}
		String tranId = getTc();
		
		session.setAttribute("tranId", tranId);
		session.setAttribute("tranDesc", tranDesc);
		
		/*
		 * for: bypass SubMenu screen and go Tc screen directly
		 * e.g.
		 * 1. input TC no. in MainMenu screen
		 * 2. Next Transaction case
		 */
		if(session.getAttribute("menuId") == null){
			Menu menu = service.findMenuByTranId(tranId);
			session.setAttribute("menuId", menu.getMenuId());
			session.setAttribute("menuDesc", menu.getMenuDesc());
		}
		
		String menuId = (String)session.getAttribute("menuId");
		String menuDesc = (String)session.getAttribute("menuDesc");
		
		String enter_Btn = HotkeyUtil.getEnterBtn();
		String f3_Btn = HotkeyUtil.getGoSubMenuBtn( menuId, menuDesc, request.getContextPath());
		model.addAttribute("headerBtns",HotkeyUtil.getPrintBtn());
		model.addAttribute("bodyBtns",enter_Btn + f3_Btn);
		model.addAttribute("title", tranDesc);
		model.addAttribute("tranId", tranId);
		preInput(model);
		return "transaction/"+getTc()+"/input";
	}
	
	@RequestMapping(value="/detail", method = RequestMethod.POST)
	protected String detail(HttpServletRequest request, HttpSession session, ModelMap model) {
		System.out.println("=================detail"+request.getParameterMap());
		String menuId = (String)session.getAttribute("menuId");
		String menuDesc = (String)session.getAttribute("menuDesc");
		String tranId = (String)session.getAttribute("tranId");
		String tranDesc = (String)session.getAttribute("tranDesc");
		
		String contextPath = request.getContextPath();
		String f1_Btn = HotkeyUtil.getGoNewTcBtn(tranId, tranDesc, contextPath);
		String f3_Btn = HotkeyUtil.getGoSubMenuBtn(menuId, menuDesc, contextPath);
		String f8_Btn = HotkeyUtil.getUpdBtn();
		String f12_Btn = HotkeyUtil.getGoFstScreenBtn(tranId, tranDesc, contextPath);
		
		model.addAttribute("headerBtns",HotkeyUtil.getPrintBtn());
		model.addAttribute("bodyBtns",f1_Btn + f3_Btn+f8_Btn + f12_Btn);
		model.addAttribute("title", tranDesc);
		model.addAttribute("tranId", getTc());
		preDetail(request, session, model);
		
		if (model.get(DemoConstant.REDIRECT_UTL) != null){
			return model.get(DemoConstant.REDIRECT_UTL).toString();
		} 
		return "transaction/"+getTc()+"/detail";
	}
	
	@RequestMapping(value="/done", method = RequestMethod.POST)
	protected String done(HttpServletRequest request, HttpSession session,ModelMap model) {
		System.out.println("=================done"+request.getParameterMap());
		String menuId = (String)session.getAttribute("menuId");
		String menuDesc = (String)session.getAttribute("menuDesc");
		String tranId = (String)session.getAttribute("tranId");
		String tranDesc = (String)session.getAttribute("tranDesc");
		
		String contextPath = request.getContextPath();
		String f1_Btn = HotkeyUtil.getGoNewTcBtn(tranId, tranDesc, contextPath);
		String f3_Btn = HotkeyUtil.getGoSubMenuBtn(menuId, menuDesc, contextPath);
		String f12_Btn = HotkeyUtil.getGoFstScreenBtn(tranId, tranDesc, contextPath);
		model.addAttribute("headerBtns",HotkeyUtil.getPrintBtn());
		model.addAttribute("bodyBtns",f1_Btn + f3_Btn);
		model.addAttribute("title", tranDesc);
		model.addAttribute("tranId", tranId);
		return "transaction/done";
	}
}
